package com.timepies.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.timepies.domain.Pie;

public class IntervalloDataBuilder {

	private List<Pie> listPie;

	public IntervalloDataBuilder(InputStream csv) throws Exception {
		listPie = new ArrayList<Pie>();
		InputStreamReader is = new InputStreamReader(csv);
		BufferedReader reader = new BufferedReader(is);
		reader.readLine();
		String pieLine;
		while ((pieLine = reader.readLine()) != null)
			listPie.add(new Pie(pieLine));
	}

	public List<Pie> getGroups(Intervallo interval) {
		Map<String, Pie> map = new HashMap<String, Pie>();
		Collections.shuffle(listPie, new Random(System.nanoTime()));
		List<Pie> subList = listPie.subList(0, 10);
		for (Pie pie : subList)
			map.put(pie.getCategoria(), pie);
		return new ArrayList<Pie>(map.values());
	}

	public List<Pie> getListPie() {
		return listPie;
	}

}
